const { merge } = require('webpack-merge')
const baseConfig = require('./webpack.common')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')

module.exports = merge(baseConfig, {
  mode: 'production',
  devtool: 'none',
  optimization: {
    usedExports: true,
    minimize: true, // 压缩代码
    splitChunks: { // 代码分割
      cacheGroups: {
        // 将所有的公共模块压缩到一起
        commons: {
          test: /[\\/]node_modules[\\/]/,
          name: 'vendor',
          chunks: 'all'
        }
      }
    }
  },
  module: {
    rules: [
      {
        test: /\.css?$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader']
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(), // 清楚打包后文件的插件
    new MiniCssExtractPlugin({ // 将css 的代码合并到一个文件中
      filename: 'main.css'
    }),
    // 将public目录中的所有文件复制到dist目录下
    new CopyWebpackPlugin({
      patterns: [
        {
          from: 'public/',
          to: 'public',
          toType: 'dir'
        }
      ]
    })
  ]
})